package com.cqie.tes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqie.tes.comm.R;
import com.cqie.tes.domain.Evalutetarget;
import com.cqie.tes.domain.Evalutetype;
import com.cqie.tes.domain.Result;
import com.cqie.tes.domain.vo.tes.top.*;
import com.cqie.tes.mapper.EvalutetargetMapper;
import com.cqie.tes.mapper.EvalutetypeMapper;
import com.cqie.tes.mapper.ResultMapper;
import com.cqie.tes.service.ECharService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.reflect.generics.tree.Tree;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ECharServiceImpl implements ECharService {

    @Autowired
    EvalutetargetMapper evalutetargetMapper;
    @Autowired
    EvalutetypeMapper evalutetypeMapper;
    @Autowired
    ResultMapper resultMapper;

    @Override
    public R targetTree(){
        // 获取指标树
        TreeDataVo treeDataVo = new TreeDataVo();
        treeDataVo.setName("评价指标");
        // 查询所有评价类型并放入
        List<Evalutetype> evalutetypeList = evalutetypeMapper.selectList(new QueryWrapper<Evalutetype>()
                .lambda()
                .eq(Evalutetype::getMark, 1)
                .orderByAsc(Evalutetype::getId));
        List<TreeDataVo> vos = new ArrayList<>();
        evalutetypeList.forEach(entity->{
            TreeDataVo vo = new TreeDataVo();
            vo.setName(entity.getName());

            // 放孩子
            // 一级指标列表
            List<Long> fatherIds = evalutetargetMapper.selectList(new QueryWrapper<Evalutetarget>()
                    .lambda()
                    .eq(Evalutetarget::getMark, 1)
                    .eq(Evalutetarget::getParentId,-1)
                    .eq(Evalutetarget::getTypeId,entity.getId())
                    .orderByAsc(Evalutetarget::getId))
                    .stream()
                    .map(Evalutetarget::getId)
                    .collect(Collectors.toList());

            getTree(fatherIds,vo);

            vos.add(vo);
        });
        treeDataVo.setChildren(vos);


        return R.ok().data("tree",treeDataVo);
    }

    public void getTree(List<Long> fatherIds,TreeDataVo data){
        if (fatherIds.isEmpty()){
            return;
        }
        // 查出这个所有ids包含列表
        List<Evalutetarget> entityList = evalutetargetMapper.selectList(new QueryWrapper<Evalutetarget>()
                .lambda()
                .eq(Evalutetarget::getMark, 1)
                .in(Evalutetarget::getId, fatherIds)
                .orderByAsc(Evalutetarget::getId));

        List<TreeDataVo> vos = new ArrayList<>();
        entityList.forEach(entity->{
            TreeDataVo vo = new TreeDataVo();
            vo.setName(entity.getName());

            // "initialTreeDepth": 3,
            List<Long> ids = evalutetargetMapper.selectList(new QueryWrapper<Evalutetarget>()
                    .lambda()
                    .eq(Evalutetarget::getMark, 1)
                    .eq(Evalutetarget::getParentId,entity.getId())
                    .orderByAsc(Evalutetarget::getId))
                    .stream()
                    .map(Evalutetarget::getId)
                    .collect(Collectors.toList());

            getTree(ids,vo);

            vos.add(vo);
        });
        data.setChildren(vos);

    }

    @Override
    public R barTopTen() {

        BarVo barVo = new BarVo();

        TitleVo titleVo = new TitleVo();
        titleVo.setText("评教 TOP 10");
        titleVo.setSubtext("数据来自学生评价");

        ToolTipVo toolTipVo = new ToolTipVo();
        toolTipVo.setTrigger("axis");

        LegendVo legendVo = new LegendVo();
        String s[] = {"总分", "学生评价","教师自评","教师互评","教研室评价","系部评价"};
        legendVo.setData(Arrays.asList(s));

        List<XAxisVo> xAxisVos = new ArrayList<>();
        XAxisVo xAxisVo = new XAxisVo();
        xAxisVo.setType("value");
        Double g[] = {0.0, 0.01};
        xAxisVo.setBoundaryGap(Arrays.asList(g));
        xAxisVos.add(xAxisVo);

        // TODO
        // ===================================================
        // 取出这次任务所有记录
        Long taskId = 1L;
        List<Result> resultList = resultMapper.selectList(new QueryWrapper<Result>()
                .lambda()
                .eq(Result::getTaskId, taskId.intValue()));

        // 存分
        Map<Long,Double> score = new HashMap<>();
        // 5种评价的分
        List<Double> t1 = new ArrayList<>();
        List<Double> t2 = new ArrayList<>();
        List<Double> t3 = new ArrayList<>();
        List<Double> t4 = new ArrayList<>();
        List<Double> t5 = new ArrayList<>();
        // 名字列表
        List<String> name = new ArrayList<>();
        // 合并所有相同的
        resultList.forEach(result -> {
            score.put(result.getId(),score.get(result.getId()) == null?result.getScore():result.getScore()+score.get(result.getId()));
        });
        // ===================================================



        YAxisVo yAxisVo = new YAxisVo();
        List<YAxisVo> yAxisVos = new ArrayList<>();
        yAxisVo.setType("category");
        String n[] = {"张正龙","赖敏","刘洋","刘胜会","付祥明"};
        yAxisVo.setData(Arrays.asList(n));
        yAxisVos.add(yAxisVo);

        List<SerieVo> serieVos = new ArrayList<>();
        SerieVo serieVo1 = new SerieVo();
        Double data1[]  = {52.0,64.0,76.0,88.0,92.0};
        serieVo1.setName("总分");
        serieVo1.setData(Arrays.asList(data1));


        SerieVo serieVo2 = new SerieVo();
        Double data2[]  = {23.0,17.0,14.0,15.0,28.0,};
        serieVo2.setName("学生评价");
        serieVo2.setData(Arrays.asList(data2));


        SerieVo serieVo3 = new SerieVo();
        Double data3[]  = { 21.0,12.0,14.0,19.0,27.0};
        serieVo3.setName("教师自评");
        serieVo3.setData(Arrays.asList(data3));


        SerieVo serieVo4 = new SerieVo();
        Double data4[]  = {    17.0,10.0,11.0,16.0,27.0};
        serieVo4.setName("教师互评");
        serieVo4.setData(Arrays.asList(data4));


        SerieVo serieVo5 = new SerieVo();
        Double data5[]  = {    13.0,10.0,9.0,12.0,25.0};
        serieVo5.setName("教师互评");
        serieVo5.setData(Arrays.asList(data5));


        SerieVo serieVo6 = new SerieVo();
        Double data6[]  = {    17.0,5.0,1.0,5.0,29.0};
        serieVo6.setName("教师互评");
        serieVo6.setData(Arrays.asList(data6));

        serieVos.add(serieVo1);
        serieVos.add(serieVo2);
        serieVos.add(serieVo3);
        serieVos.add(serieVo4);
        serieVos.add(serieVo5);
        serieVos.add(serieVo6);






        barVo.setTitle(titleVo);
        barVo.setToolTip(toolTipVo);
        barVo.setLegend(legendVo);
        barVo.setXAxis(xAxisVos);
        barVo.setYAxis(yAxisVos);
        barVo.setSeries(serieVos);

        return R.ok().data("bar",barVo);
    }

    @Override
    public R tableTopSeven() {
        // ===================================================
        // 取出这次任务所有记录
        Long taskId = 1L;
        List<Result> resultList = resultMapper.selectList(new QueryWrapper<Result>()
                .lambda()
                .eq(Result::getTaskId, taskId.intValue()));

        // 存分
        Map<Integer,Double> score = new HashMap<>();
        // 名字列表
        List<String> name = new ArrayList<>();
        // 合并所有相同的
        resultList.forEach(result -> {
            score.put(result.getTypeId(),score.get(result.getTypeId()) == null?result.getScore():result.getScore()+score.get(result.getTypeId()));

        });
        // ===================================================

        return R.ok();
    }
}
